package web.servlet;

import domain.Account;
import domain.Product;
import service.AccountService;
import service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewAccountServlet extends HttpServlet {
    private static final String NEW_ACCOUNT = "/WEB-INF/jsp/account/newAccount.jsp";

    AccountService accountService = new AccountService();
    CatalogService catalogService = new CatalogService();
    private Account newAccount = new Account();
    private String username;
    private String password;
    private String rpassword;
    private String firstName;
    private String lastName;
    private String email;
    private String phone;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String favouriteCategoryId;
    private String languagePreference;
    private boolean listOption;
    private boolean bannerOption;
    private String bannerName;
    private String msg;
    private List<Product> myList;
//    private boolean authenticated;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.rpassword = req.getParameter("rpassword");
        this.firstName = req.getParameter("firstName");
        this.lastName = req.getParameter("lastName");
        this.email = req.getParameter("email");
        this.phone = req.getParameter("phone");
        this.address1 = req.getParameter("address1");
        this.address2 = req.getParameter("address2");
        this.city = req.getParameter("city");
        this.state = req.getParameter("state");
        this.zip = req.getParameter("zip");
        this.country = req.getParameter("country");
        this.languagePreference = req.getParameter("languagePreference");
        this.favouriteCategoryId = req.getParameter("favouriteCategoryId");
        if (req.getParameter("listOption").equals("1")) {
            this.listOption = true;
        } else {
            this.listOption = false;
        }
        if (req.getParameter("bannerOption").equals("1")) {
            this.bannerOption = true;
        } else {
            this.bannerOption = false;
        }

        if (!validate()) {
            req.setAttribute("newMsg", this.msg);
            req.getRequestDispatcher(NEW_ACCOUNT).forward(req, resp);
        } else {
//            //检测用户是否已经存在
//            newAccount = accountService.getAccountByName(username);
//            if (newAccount != null) {
//                req.setAttribute("newMsg", this.msg = "用户名已存在");
//                req.getRequestDispatcher(NEW_ACCOUNT).forward(req, resp);
//            } else {
            Account newAccount1 = new Account();
            newAccount1.setUsername(this.username);
            newAccount1.setPassword(this.password);
            newAccount1.setFirstName(this.firstName);
            newAccount1.setLastName(this.lastName);
            newAccount1.setEmail(this.email);
            newAccount1.setPhone(this.phone);
            newAccount1.setAddress1(this.address1);
            newAccount1.setAddress2(this.address2);
            newAccount1.setCity(this.city);
            newAccount1.setState(this.state);
            newAccount1.setZip(this.zip);
            newAccount1.setCountry(this.country);
            newAccount1.setLanguagePreference(this.languagePreference);
            newAccount1.setFavouriteCategoryId(this.favouriteCategoryId);
            newAccount1.setStatus("OK");
            newAccount1.setListOption(this.listOption);
            newAccount1.setBannerOption(this.bannerOption);
//                authenticated = true;
            accountService.insertAccount(newAccount1);//往数据库里面插一个
            newAccount = accountService.getAccountByName(newAccount1.getUsername());
            myList = catalogService.getProductListByCategory(newAccount1.getFavouriteCategoryId());
            HttpSession session = req.getSession();
            session.setAttribute("newAccount", newAccount1);
            //注册时选择了显示商品列表 (listOption 为 true)，则会根据用户喜欢的商品类别，获取该类别下的商品列表，并将其存储到 session 中，供后续使用。
            if (newAccount1.isListOption()) {
                CatalogService catalogService = new CatalogService();
                List<Product> myList = catalogService.getProductListByCategory(newAccount1.getFavouriteCategoryId());
                session.setAttribute("myList", myList);
            }
            resp.sendRedirect("signOnForm");
        }
    }

    //检测非法的函数，并存入错误信息
    private boolean validate() {
//        if (this.username == null || this.username.equals("")) {
//            this.msg = "用户名不能为空";
//            return false;
//        }
//        if (this.password == null || this.password.equals("")) {
//            this.msg = "密码不能为空";
//            return false;
//        }
//        if (this.rpassword == null || this.rpassword.equals("")) {
//            this.msg = "确认密码不能为空";
//            return false;
//        }
//        if (!this.rpassword.equals(this.password)) {
//            this.msg = "两次密码输入不相同";
//            return false;
//        }
        if (this.firstName == null || this.firstName.equals("")) {
            this.msg = "firstName不能为空";
            return false;
        }
        if (this.lastName == null || this.lastName.equals("")) {
            this.msg = "lastName不能为空";
            return false;
        }
        if (this.email != null && !this.email.equals("")) {
            if (!this.email.matches("[\\w]+@[\\w]+.[\\w]+[\\w]")) {
                msg = "请输入有效的邮箱地址";
                return false;
            }
        } else {
            msg = "邮箱不能为空";
            return false;
        }
        if (this.phone == null || this.phone.equals("")) {
            msg = "电话不能为空";
            return false;
        }
        if (this.address1 == null || this.address1.equals("")) {
            this.msg = "地址一不能为空";
            return false;
        }
        if (this.address2 == null || this.address2.equals("")) {
            this.msg = "地址二不能为空";
            return false;
        }
        if (this.city == null || this.city.equals("")) {
            this.msg = "city不能为空";
            return false;
        }
        if (this.state == null || this.state.equals("")) {
            this.msg = "state不能为空";
            return false;
        }
        if (this.zip == null || this.zip.equals("")) {
            this.msg = "zip不能为空";
            return false;
        }
        if (this.country == null || this.country.equals("")) {
            this.msg = "Country不能为空";
            return false;
        }
        return true;
    }
}

